/**
 * 构建 by lee
 */
var pkg = require('./package.json');

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var minify = require('gulp-minify-css');
var header = require('gulp-header');
var del = require('del');

//获取参数，注释，构建的目标目录，发行版本目录，任务
var argv = require('minimist')(process.argv.slice(2), {
    default: {
        ver: 'all'
    }
}), note = [
    '/** <%= pkg.name %>-v<%= pkg.version %> <%= pkg.license %> License By <%= pkg.homepage %> */\n <%= js %>'
    , {pkg: pkg, js: ';'}
], distDir = './dist', releaseDir = './output_scm/' + pkg.name, task = {
    //压缩 JS
    compressJs: function () {
        console.log('============compress js============');
        var src = [
            './src/**/*.js', '!./src/config.js', '!./src/lib/extend/echarts.js'
            , '!./src/lib/extend/dtree.js', '!./src/lib/extend/echartsTheme.js'
        ];
        return gulp.src(src).pipe(uglify()).pipe(header.apply(null, note)).pipe(gulp.dest(distDir));
    }

    //压缩 CSS
    , compressCss: function () {
        console.log('============compress css============');
        var src = [
            './src/**/*.css'
        ], noteNew = JSON.parse(JSON.stringify(note));
        noteNew[1].js = '';
        return gulp.src(src).pipe(minify({
            compatibility: 'ie7'
        })).pipe(header.apply(null, noteNew)).pipe(gulp.dest(distDir));
    }

    //移动文件夹
    , mv: function () {
        console.log('============move src to dist============');
        gulp.src('./src/index.js').pipe(gulp.dest(distDir));
        gulp.src('./src/config.js').pipe(gulp.dest(distDir));
        gulp.src('./src/lib/**/*').pipe(gulp.dest(distDir + '/lib'));
        gulp.src('./src/style/**/*').pipe(gulp.dest(distDir + '/style'));
        return gulp.src('./src/views/**/*').pipe(gulp.dest(distDir + '/views'));
    }
};


//清理
gulp.task('clear', async function (cb) {
    console.log('============clean old dist============');
    return del(['./dist/*'], cb);
});

gulp.task('compressJs', task.compressJs);
gulp.task('compressCss', task.compressCss);
gulp.task('mv', task.mv);

//发行
gulp.task('release', async function () {
    //复制核心文件
    console.log('============copy core file============');
    gulp.src('./dist/**/*').pipe(gulp.dest(releaseDir + '/dist'));
    gulp.src('./start/index.html').pipe(gulp.dest(releaseDir + '/start'));
    gulp.src('./start/json/**/*').pipe(gulp.dest(releaseDir + '/start/json'));
    gulp.src('./start/module/**/*').pipe(gulp.dest(releaseDir + '/start/module'));
    //复制框架核心文件
    console.log('============copy frame core file============');
    return gulp.src('./start/layui/**/*').pipe(gulp.dest(releaseDir + '/start/layui'));
});

gulp.task('default', gulp.series('clear', 'compressJs', 'compressCss', 'mv', 'release', func => func()));



